package com.jlnk.glidedemo.presenter;

import com.jlnk.glidedemo.client.RetrofitClient;
import com.jlnk.glidedemo.entity.ImageEntity;
import com.jlnk.glidedemo.service.ApiService;
import com.jlnk.glidedemo.views.ImageListView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ImageListPresenter {
    private ImageListView view;
    private ApiService service;
    public ImageListPresenter(ImageListView view) {
        this.view = view;
        service = RetrofitClient.getRetrofitInstance().create(ApiService.class);
    }
    public void getImages() {
        service.getImages().enqueue(new Callback<List<ImageEntity>>() {
            @Override
            public void onResponse(Call<List<ImageEntity>> call, Response<List<ImageEntity>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    view.showImages(response.body());
                } else {
                    view.showError();
                }
            }

            @Override
            public void onFailure(Call<List<ImageEntity>> call, Throwable t) {
                view.showError();
            }
        });
    }
    public void openImageDetail(String imageUrl) {
        view.openImageDetail(imageUrl);
    }
}
